home *** CD-ROM | disk | FTP | other *** search
/ ftp.whtech.com / ftp.whtech.com.tar / ftp.whtech.com / datasheets and manuals / Hardware / tmsposting.txt < prev    next >
Text File  |  2006-10-19  |  18KB  |  382 lines

  1. Subject: Re: Z80 emulator to learn assembly?
  2. From: Paul Urbanus <urb@onramp.net>
  3. Date: 1997/01/31
  4. Message-ID: <32F28691.2ADC@onramp.net>
  5. References: <32edd486.3490767@news.airmail.net> <5cgr4r$nqb@hecate.umd.edu> <5cipc6$ra9@dinkel.civ.utwente.nl>
  6. Content-Type: text/plain; charset=us-Ascii
  7. Organization: OnRamp Technologies; ISP;  Dallas/Ft Worth/Houston, TX USA
  8. Mime-Version: 1.0
  9. Newsgroups: comp.emulators.misc,comp.os.cpm
  10. X-Mailer: Mozilla 3.01 (Win16; I)
  11.  
  12.  
  13.  
  14. Marcel de Kogel wrote:
  15. >
  16. > On 26 Jan 1997 23:59:23 GMT, marat@Glue.umd.edu (Marat Fayzullin)
  17. > wrote:
  18. >
  19. > >Rogers Cadenhead (rcade@airmail.net) wrote:
  20. > >: I am learning Z80 assembly language programming so that I can write
  21. > >: some new Colecovision games and figure out how some of my old
  22. > >: favorites were written.
  23. > >
  24. > >: What's the best Z80 emulator I can find for DOS or Win95 that I can
  25. > >: use to run the programs I'm writing? I've read that CPM emulators are
  26. > >: the best choice.
  27. > >As you are going to write Colecovision programs, a crossassembler+ColEm
  28. > >combination will probably be the best. Also, check AdamEm, the Coleco
  29. > >Adam emulator by Marcel de Kogel.
  30. > >
  31. > >Marat
  32. >
  33. > I'm working on some as well, and while writing them I found some very
  34. > interesting features in the VDP design (e.g. there's no such thing as
  35. > truly seperate read and write addresses) I didn't find described
  36. > anywhere. While I've implemented most in ADAMEm, I didn't find out how
  37. > some of this really works (e.g. I get mixed results when reading VRAM
  38. > after setting a new write address). This is why I prefer using Mission
  39. > and an MSX for testing purposes; In fact, it's why I wrote Mission in
  40. > the first place. Of course, final testing is done on the CV itself,
  41. > and you'll need an MSX to run Mission natively
  42. >
  43. > Marcel
  44.  
  45. Marcel,
  46.  
  47. The VDP (Video Display Processor) chip used in the Colecovision and in
  48. the early (MSX-1?) systems was the Texas Instruments TMS9918A, which was
  49. also used in the TI99/4A Home Computer. This machine came onto the
  50. market in 1980, in the midst of the video game/home computer boom.
  51. During that time, I worked for TI as a student, and in 1982 I
  52. co-authored (along with Jim Dramis) a game for the 99/4A called PARSEC,
  53. among other things. All of us game programmers always lamented the fact
  54. that the VDP memory was 'indirectly' mapped instead of direct, which of
  55. course limited the amount of raw bit pushing we could do. Anyway, I
  56. think the following will (hopefully) clear up your confusion regarding
  57. accessing VDP memory. Note that later versions of the MSX systems
  58. (MSX-2?) used a superset of the TI9918A, the YM9938, which was made by
  59. Yamaha. The following discussion applies only to the TI9918A VDP chip.
  60.  
  61. You are correct when you stated that there is only ONE memory address
  62. register in the VDP, and this is used for both reading and writing data.
  63. Thus, there must be a way to indicate to the VDP whether the address
  64. which has been written is to be used for reading or writing data. This
  65. is done by using one of the upper address bits in the 16 bit address.
  66.  
  67. Since the 9918A can only address 16k bytes of memory, the upper two bits
  68. in the address (A14-A15) will always be zero. While bit 15 (the most
  69. significant) is always set to zero, bit 14 is used to distinguish
  70. between a read and a write address. The following shows how this bit
  71. affects subsequent VRAM data accesses.
  72.  
  73. VRAM address bit 14  |         VRAM data access function
  74. ------------------------------------------------------------------------
  75.          0           |   VRAM address specifies location to read
  76.                      | (initiate the read/increment the address counter)
  77. ------------------------------------------------------------------------
  78.          1           |  VRAM address specifies location to write
  79.                      | (wait for data write to VDP before actual write
  80.                      |  to VRAM, then increment address counter)
  81.  
  82. If you are simply calculating the address for writing data, then using
  83. that address as the write address without setting bit 14=1, this might
  84. cause some unexpected behavior. If bit 14=0, this will cause the VDP to
  85. initiate a read cycle and then increment the address counter, thus
  86. giving the impression that the "write address" has been set to
  87. (address+1). As I stated before, there really is only one address
  88. register, so when you perform data reads/writes you are affecting the
  89. same register.
  90.  
  91. I'm sure that the VDP designers (at TI, anyway) didn't expect people to
  92. interleave data reads and writes without resetting the address, so any
  93. undocumented operation may or may not be supported on all revisions of
  94. the chip. As will any 'undocumented bugs/features', I'd be concerned
  95. about the implementation of these 'features' in 9918A clones, such as
  96. the Yamaha 9938.
  97.  
  98. Another thing you should be aware of is the timing constraints placed on
  99. address and data accesses to the VDP RAM. Actual reading/writing of the
  100. VDP RAM (VRAM) by the CPU can only occur when the VDP is not reading the
  101. memory for the purpose of generating the screen image. In some display
  102. modes, most of the memory bandwidth is utilized for generating the
  103. image, leaving little time (unfortunately) for the CPU to access memory.
  104. The worst case scenario is in graphics modes I,II, where the VDP uses
  105. almost all of the memory bandwidth to generate the screen image. In this
  106. mode, only 1 memory access out of 16 is designated for the CPU - the
  107. rest are allocated for screen refresh.
  108.  
  109. According to the 9918A (VDP) Data Manual, there are two timing
  110. constraints to be followed when access VRAM.
  111.  
  112. 1. After the second address byte (MSByte) has been written to the VDP,
  113. there must be a 2 microsecond wait before any data read/write accesses
  114. can occur. This constraint ALWAYS applies, no matter which display mode
  115. is in effect or which part of the screen (active video, vertical
  116. sync/blanking) is being displayed. In the table below, this is referred
  117. to as 'VDP Delay'.
  118.  
  119. 2. The second timing constraint depends on which display mode is active
  120. in the VDP, and which part of the screen (active video, vertical
  121. sync/blanking) is being displayed. The following table shows these
  122. timing constraints. In the table, this second delay constraint is
  123. referred to as 'Time waiting for an access window'.
  124.  
  125.                     |            |  VDP  | Time waiting for |  Total
  126.    Condition        |    Mode    | Delay | an access window |  time
  127. ------------------------------------------------------------------------
  128. Active Display Area |   Text     | 2 us  |   0  -  1.1  us  | 2 - 3.1 us
  129. ------------------------------------------------------------------------
  130. Active Display Area |  Graphics  | 2 us  |   0  -  5.95 us  | 2 - 8   us
  131.                     |    I,II    |       |                  |
  132. ------------------------------------------------------------------------
  133. 4300 us after       |    All     | 2 us  |      0       us  |   2     us
  134. Vertical Interrupt  |            |       |                  |
  135. ------------------------------------------------------------------------
  136. Register 1, bit 1=0 |    All     | 2 us  |      0       us  |   2     us
  137. (display is blanked)|            |       |                  |
  138. ------------------------------------------------------------------------
  139. Active Display Area | Multicolor | 2 us  |    0  -  1.5 us  | 2 - 3.5 us
  140. ------------------------------------------------------------------------
  141.  
  142. Examination of the above access window table yields the following
  143. observations.
  144.  
  145. 1. Always try to do massive VRAM moves during the vertical retrace
  146. period, since that is when max memory bandwidth is available to the CPU,
  147. theoretically 500 Kbytes/sec. This is especially important in Graphics
  148. modes I & II, which will be used for almost ALL games. Theoretically,
  149. one can move (4300 us/2 us) 2150 bytes to/from the VRAM in one vertical
  150. blanking time.
  151.  
  152. 2. If you need to move lots of data, such as completely changing
  153. screens, set the blanking bit it VDP register 1 to 0, then read/write
  154. the data.
  155.  
  156.  
  157.    WHY DOES THE VDP NEED SO MUCH BANDWIDTH TO REFRESH THE SCREEN,
  158.    AND OTHER STUFF YOU REALLY DON'T NEED TO KNOW ABOUT THE 9918A?
  159.    --------------------------------------------------------------
  160.  
  161. The following is provided as additional background information, and may
  162. be considered excess, but I give for it those who might want to
  163. understand how the bandwidth is used in Graphics modes I & II.
  164.  
  165. First, consider the overriding considerations for the guys who did the
  166. 9918A chip design. Of course, the part must function, but more
  167. importantly, the die size must be as small as possible to keep the cost
  168. down. After all, this chip was targeted toward a consumer market.
  169.  
  170. Now, a little background on VDP memory and pixel timing. The master
  171. clock for the VDP is the color burst frequency X 3. All subsequent
  172. calculations are for the NTSC version of the part, although the PAL
  173. numbers will be similar. The color burst frequency is 3.579545 MHz.
  174. While I don't know pi to this many digits, the color burst frequency is
  175. very handy to know when working with NTSC video. So, the master clock
  176. frequency is given by
  177.  
  178. Fmaster =  Fcolorburst * 3
  179.         =  3.579545 MHz * 3
  180.         = 10.7386 MHz
  181.  
  182. The period of the master clock is given by
  183.  
  184. Tmaster = 1/Fmaster
  185.         = 1/10.7386 MHz
  186.         = 93.12 ns (nanoseconds)
  187.  
  188. Each memory access takes four master clock times, so the memory access
  189. time is given by
  190.  
  191. Tmem    = Tmaster * 4
  192.         = 93.12 ns * 4
  193.         = 372.5 ns = 0.3725 us
  194.  
  195. The horizontal line time, or the amount of time from the start of one
  196. horizontal display line to the next horizontal display line is specified
  197. in the data sheet as Thorz = 63.695 us. So, the total number of times
  198. which VDP memory can be accessed in a single horizontal scan line is
  199. given by
  200.  
  201. Mhorz  = Max number of memory accesses in a horizontal line
  202.        = Thorz/Tmem
  203.        = 63.695 us/0.3725 us
  204.        = 171 memory access per horizontal line, max
  205.  
  206. We now know how many memory accesses are available to be allocated for
  207. display refresh and CPU accesses combined.
  208.  
  209. Next, let's find out how many memory accesses are requied to build up a
  210. single horizontal scan line in Graphics modes I or II. Any unused
  211. accesses can theoretically be allocated to the CPU.
  212.  
  213. Any one active scan line is composed of up to six layers of graphic data
  214. (listed in back to front hierarchy):
  215.  
  216. 1. Background color (from VDP register #7)
  217. 2. Character pattern/color info
  218. 3. Sprites (min number=0, max number=4)
  219.    NOTE: there may never be more than 4 sprites on a horizontal scan
  220. line
  221.  
  222. Let's see how many memory accesses are required to get the data for the
  223. three different 'planes' described above.
  224.  
  225. First, the background color requires zero memory accesses, as it is held
  226. the lower 4 bits of VDP register 7.
  227.  
  228. Next, is the character data. There are 32 characters per scan line, and
  229. each character in the scan line requires the following memory accesses
  230. to retrieve the data required to generate the pixel data for that
  231. character.
  232.  
  233. 1. Read character number from Pattern Name Table (PNT)
  234. 2. Read character bitmap data from Pattern Generator Table (PGT)
  235. 3. Read character color info from Pattern Color Table (PCT)
  236.  
  237. As you can see, it takes three memory accesses for each character, and
  238. so the total number of memory accesses required per scan line to build
  239. up the character display plane is given by
  240.  
  241. Mchar = 32 characters/scan line X 3 mem accesses/character
  242.       = 96 memory accesses per scan line for character plane
  243.  
  244. Finally, the sprite planes must be processed. The 9918A allows up to
  245. four sprites (out of 32) to be displayed on a scan line, and sprite #0
  246. has the highest priority - that is, it will be the frontmost.
  247.  
  248. To determine which sprite will be visible on any given scan line, the
  249. Y-position of all 32 sprites must be read from the Sprite Attribute
  250. Table (SAT) in VRAM and compared against the current scan line number.
  251. When doing the compare, the Mag bit from VDP register 1 must be taken
  252. into account, since the magnification is in both the x and y directions.
  253.  
  254. If the Y-location of the sprite is such that it is to be displayed on
  255. this scan line, then the sprite number (0-32) is placed in one of four
  256. temporary holding registers (SR0-SR3), if all four registers are not
  257. already filled. SR0 fills first, SR3 fills last, and SR0 specifies the
  258. frontmost sprite plane and SR3 specifies the rearmost sprite plane.
  259. While the Y-locations of these active sprites may be saved inside the
  260. VDP, I suspect they are not. Keeping these Y-locations would require 4
  261. extra holding registers, which can be eliminated by refetching the
  262. Y-locations later, albeit at the 'cost' of more memory access. However,
  263. 4 registers affects the chip die size, but it is not clear that the VDP
  264. user even knows about the 'cost' of these extra memory cycles.
  265.  
  266. In the worst case, the first 28 sprites, 0-27, are not displayed on a
  267. given scan line, but sprites 28-31 will be displayed. In this case, the
  268. Y-location all 32 sprites may have to be read. For the purposes of
  269. memory access calculations, we must assume that all 32 sprite
  270. Y-locations will have to be read. Therefore, we define the number of
  271. memory accesses required to test which sprites should be displayed on a
  272. given scan line as
  273.  
  274. Msprite_test = 32 memory cycles (1 Y-location per sprite)
  275.  
  276. After it is determined which sprites need to be displayed, the data for
  277. the four sprites (again, worst case) to be displayed must be fetched.
  278. For each sprite, there are 4 bytes (Y-location, X-location, pattern
  279. number, color/early clock) which need to be fetched from the Sprite
  280. Attribute Table. When the Size bit in VDP Register is set to 1,
  281. indicating double size sprites, two bytes of sprite pattern data must be
  282. read from the Sprite Pattern Generator Table. Again, this is the worst
  283. case. Therefore, six memory cycles are required for each sprites which
  284. is to be displayed. So, we now define the maximum number of memory
  285. cycles required to fetch the data needed to display four sprites on a
  286. scan line as
  287.  
  288. Msprite_data = 4 sprites line x 6 memory cycles/sprite
  289.              = 24 memory cycles
  290.  
  291. Now, let's summarize the maximum total number of accesses required for
  292. displaying four sprites on a scan line as
  293.  
  294. Msprite = total number of memory accesses per scan line for sprite
  295. display
  296.         = test which sprites are on this line + sprite display data
  297. access
  298.         = Msprite_test + Msprite_data
  299.         =     32       +     24
  300.         = 56 memory cycles
  301.  
  302. Whew! Finally, we can calculate the number of memory cycles used to
  303. refresh one active scan line in the display. This is given by
  304.  
  305. Mdisplay = Mchar + Msprite
  306.          =  96   +   56
  307.          = 152
  308.  
  309. For those of you who I have not totally confused, the end is now in
  310. sight. We are ready to compute the number of memory cycles available to
  311. the CPU.
  312.  
  313. Drum roll, please!
  314.  
  315. Mcpu  = Mem accesses in one horizontal scan line - display mem accesses
  316.       = Mhorz - Mdisplay
  317.       =  171  -   152
  318.       = 19 memory accesses available for the CPU
  319.  
  320. If the CPU can access the memory every 5.95 us, then the total number of
  321. CPU accesses allowed in a horizontal line time is given by
  322.  
  323. Mcpu_horz = horizontal line time/memory access time
  324.           = 63.695 us/5.95 us
  325.           = 10.7 CPU memory access per horizontal scan line
  326.  
  327. If one rounds 10.7 up to 11, that would seem to indicate that there are
  328. 8 memory cycles (19-11=8) which are unused.
  329.  
  330. Perhaps those extra 8 cycles could have been used to allow a fifth
  331. sprite on a line, since each sprite costs a total of seven memory cycles
  332. (1 for y-test, then 6 more if displayed). However, that would only leave
  333. one memory cycle to spare. Also, there are scheduling and sycronization
  334. issues involved regarding the sprites, and it would have probably
  335. required too much chip area to squeeze in that one extra sprite.
  336.  
  337. Or, maybe those 19 cycles could have been all allocated to CPU accesses.
  338. However, remember the earlier statement that every 16th memory accesses
  339. is allocated for the CPU. It is relatively simple (cheap) to decode this
  340. CPU access slot from the horizontal counter inside of the VDP which is
  341. used for overall horizontal timing. If, instead, we take the 19 cycles
  342. and divide them into the horizontal line time, we get
  343.  
  344. Taccess_best = 63.695 us/19 memory cyles
  345.              = 3.35 us between CPU data accesses
  346.  
  347. If memory cyclces take 372.5 ns, then the CPU could have every ninth
  348. memory cycle (3.35/0.3725). Since this is not an integral power of two,
  349. a separate CPU access counter would be required and would take more chip
  350. area (cost) than a simple decode of the lower four bits of the
  351. horizontal counter.
  352.  
  353. To summarize, the sprites take up slightly more than 1/3 of the display
  354. bandwidth. Unfortunately, the chip designers did not incude a way to
  355. turn off the sprites and thus allow 1 of four memory accesses to be
  356. allocated to the CPU.
  357.  
  358. I hope this information will be useful or educational to someone out
  359. there. Maybe you now have a better understanding of how the video
  360. hardware in the Colecovision works.
  361.  
  362. Paul Urbanus
  363. urb@urbonix.com
  364.  
  365.  
  366. P.S. In anticipation of doing some work for the Colecovision, I built a
  367. single-board computer (SBC) that used the TI processor. This SBC
  368. attached to the expansion port of the Colecovision and used DMA (Direct
  369. Memory Access) to access the hardware. Since we already had a debugger
  370. written for the TI99/4A, I modified it for the SBC so we could learn how
  371. to access the Colecovision hardware. HERE'S THE PERVERSE PART - since we
  372. didn't know any Z-80 assembly, we wanted to examine some of the code in
  373. Coleco games. So we wrote a symbolic Z80 dissassembler IN TI9900
  374. ASSEMBLY LANGUAGE. What were we thinking???
  375.  
  376.  
  377. ***                                                                ***
  378. *                 Paul Urbanus    urb@urbonix.com                    *
  379. *                                                                    *
  380. *   Never wrestle with a hog - you get dirty and the hog likes it.   *
  381. ***                                                                ***
  382.